package org.ycx.generator.code;

import org.ycx.common.util.ClassUtil;
import org.ycx.generator.GeneratorTemplate;
import org.ycx.generator.step.ControllerStep;
import org.ycx.generator.step.MapperStep;
import org.ycx.generator.step.ServiceImplStep;
import org.ycx.generator.step.ServiceStep;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by 杨 on 2017-07-19.
 */
public class AutoGenerator {

    private String module;

    private String jdbcClass;
    private String jdbcURL;
    private String jdbcUser;
    private String jdbcPwd;
    private String dataBase;
    private String tablePrefix;
    private String daoPackageName;


    public AutoGenerator(String module, String jdbcClass, String jdbcURL, String jdbcUser, String jdbcPwd, String dataBase, String tablePrefix, String daoPackageName) {
        this.module = module;
        this.jdbcClass = jdbcClass;
        this.jdbcURL = jdbcURL;
        this.jdbcUser = jdbcUser;
        this.jdbcPwd = jdbcPwd;
        this.dataBase = dataBase;
        this.tablePrefix = tablePrefix;
        this.daoPackageName = daoPackageName;
    }

    public void generatorMapper(String daoSuffix) throws Exception {
        //生成ModelAndMapper
        Set<String> models = ClassUtil.getClassName(daoPackageName, false);
        if(daoSuffix !=null && daoSuffix.length() >0){
            ModelAndMapperGenerator.setDaoSuffix(daoSuffix);
        }
        ModelAndMapperGenerator.generatorModelAndMapper(jdbcClass, jdbcURL, jdbcUser, jdbcPwd, dataBase, module, tablePrefix, daoPackageName);
    }

    /**
     * 生成Mapper,service,Controller,View
     */
    public void generatorMSCV(String... suffix) throws Exception {

        String suffixs[] = suffix;
        String daoSuffix = "-dao";
        String apiSuffix = "-service-api";
        String implSuffix = "-service-impl";
        String webSuffix = "-web";
        switch (suffixs.length) {
            case 1:
                daoSuffix = suffix[0];
                break;
            case 2:
                daoSuffix = suffix[0];
                apiSuffix = suffix[1];
                break;
            case 3:
                daoSuffix = suffix[0];
                apiSuffix = suffix[1];
                implSuffix = suffix[2];
                break;
            case 4:
                daoSuffix = suffix[0];
                apiSuffix = suffix[1];
                implSuffix = suffix[2];
                webSuffix = suffix[3];
            default:
                break;
        }
        GeneratorTemplate template = new GeneratorTemplate();

        template.setVmProject(module + daoSuffix);

        Map<String, String> context = new HashMap<>();
        context.put("package_name", daoPackageName);
        Set<String> models = models(daoPackageName + ".dao.model", "Example");

        //mapper
        template.setStep(new MapperStep());
        template.generator(module + daoSuffix, daoPackageName + ".dao.mapper", models, context);

        //Service
        template.setStep(new ServiceStep());
        template.generator(module + apiSuffix, daoPackageName + ".service", models, context);

        template.setStep(new ServiceImplStep());
        template.generator(module + implSuffix, daoPackageName + ".service.impl", models, context);


        template.setStep(new ControllerStep());
        template.generator(module + webSuffix, daoPackageName + ".web.controller", models, context);
    }

    /**
     * 生成代码
     *
     * @param suffix
     * @param pagePath
     * @throws Exception
     */
    public void generatorPage(String suffix, String pagePath, boolean override) throws Exception {
        Set<String> models = models(daoPackageName + ".dao.model", "Example");
        if (null == suffix) {
            PageCodeGeneratorUtil.generator(module + "-web", pagePath, models, override);
        } else {
            PageCodeGeneratorUtil.generator(module + suffix, pagePath, models, override);
        }

    }

    /**
     * 排除不要的规则
     *
     * @param suffix
     * @return
     */
    private Set<String> models(String packageName, String suffix) {
        Set<String> allBean = ClassUtil.getClassName(packageName, false);
        Set<String> models = new HashSet<>();
        //处理掉Example
        for (String model : allBean) {
            if (!model.endsWith(suffix)) {
                models.add(model);
            }
        }
        return models;
    }
}
